package com.sugar.psych.assessment.module.business.controller.admin.psychquestionbank;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.psych.assessment.framework.common.pojo.PageParam;
import com.sugar.psych.assessment.framework.common.pojo.PageResult;
import com.sugar.psych.assessment.framework.common.pojo.CommonResult;
import com.sugar.psych.assessment.framework.common.util.object.BeanUtils;
import static com.sugar.psych.assessment.framework.common.pojo.CommonResult.success;

import com.sugar.psych.assessment.framework.excel.core.util.ExcelUtils;

import com.sugar.psych.assessment.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.psych.assessment.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.psych.assessment.module.business.controller.admin.psychquestionbank.vo.*;
import com.sugar.psych.assessment.module.business.dal.dataobject.psychquestionbank.PsychQuestionBankDO;
import com.sugar.psych.assessment.module.business.service.psychquestionbank.PsychQuestionBankService;

@Tag(name = "管理后台 - 心理测评题库")
@RestController
@RequestMapping("/business/psych-question-bank")
@Validated
public class PsychQuestionBankController {

    @Resource
    private PsychQuestionBankService psychQuestionBankService;

    @PostMapping("/create")
    @Operation(summary = "创建心理测评题库")
    @PreAuthorize("@ss.hasPermission('business:psych-question-bank:create')")
    public CommonResult<Long> createPsychQuestionBank(@Valid @RequestBody PsychQuestionBankSaveReqVO createReqVO) {
        return success(psychQuestionBankService.createPsychQuestionBank(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新心理测评题库")
    @PreAuthorize("@ss.hasPermission('business:psych-question-bank:update')")
    public CommonResult<Boolean> updatePsychQuestionBank(@Valid @RequestBody PsychQuestionBankSaveReqVO updateReqVO) {
        psychQuestionBankService.updatePsychQuestionBank(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除心理测评题库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:psych-question-bank:delete')")
    public CommonResult<Boolean> deletePsychQuestionBank(@RequestParam("id") Long id) {
        psychQuestionBankService.deletePsychQuestionBank(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除心理测评题库")
                @PreAuthorize("@ss.hasPermission('business:psych-question-bank:delete')")
    public CommonResult<Boolean> deletePsychQuestionBankList(@RequestParam("ids") List<Long> ids) {
        psychQuestionBankService.deletePsychQuestionBankListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得心理测评题库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:psych-question-bank:query')")
    public CommonResult<PsychQuestionBankRespVO> getPsychQuestionBank(@RequestParam("id") Long id) {
        PsychQuestionBankDO psychQuestionBank = psychQuestionBankService.getPsychQuestionBank(id);
        return success(BeanUtils.toBean(psychQuestionBank, PsychQuestionBankRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得所有心理测评题库")
    @PreAuthorize("@ss.hasPermission('business:psych-question-bank:query')")
    public CommonResult<List<PsychQuestionBankRespVO>> getPsychQuestionBankAll() {
        return success(psychQuestionBankService.getPsychQuestionBankAll());
    }

    @GetMapping("/page")
    @Operation(summary = "获得心理测评题库分页")
    @PreAuthorize("@ss.hasPermission('business:psych-question-bank:query')")
    public CommonResult<PageResult<PsychQuestionBankRespVO>> getPsychQuestionBankPage(@Valid PsychQuestionBankPageReqVO pageReqVO) {
        PageResult<PsychQuestionBankDO> pageResult = psychQuestionBankService.getPsychQuestionBankPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PsychQuestionBankRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出心理测评题库 Excel")
    @PreAuthorize("@ss.hasPermission('business:psych-question-bank:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPsychQuestionBankExcel(@Valid PsychQuestionBankPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PsychQuestionBankDO> list = psychQuestionBankService.getPsychQuestionBankPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "心理测评题库.xls", "数据", PsychQuestionBankRespVO.class,
                        BeanUtils.toBean(list, PsychQuestionBankRespVO.class));
    }

}